package cool.tianyou.blue.bird.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import java.util.function.Consumer;

import cool.tianyou.blue.bird.R;
import cool.tianyou.blue.bird.constant.Constant;
import cool.tianyou.blue.bird.model.ApiResult;
import cool.tianyou.blue.bird.service.MQTTService;
import cool.tianyou.blue.bird.util.HttpUtil;
import cool.tianyou.blue.bird.util.SpUtil;
import cool.tianyou.blue.bird.util.Util;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
            findPreference("open_notification_key").setOnPreferenceClickListener(preference -> {
                Intent intent_s = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
                startActivity(intent_s);
                return true;
            });
            findPreference("logout").setOnPreferenceClickListener(preference -> {
                FragmentActivity activity = this.getActivity();
                HttpUtil.get(this.getContext(), null, "/account/logout", (Consumer<ApiResult<?>>) apiResult -> {
                    assert activity != null;
                    SpUtil.putString(activity, Constant.SP_CONSTANT.USER_ID, "");
                    SpUtil.putString(activity, Constant.SP_CONSTANT.TOKEN, "");
                    MainActivity.instance.finish();
                    activity.finish();
                    startActivity(new Intent(activity, LoginActivity.class));
                    activity.stopService(new Intent(activity, MQTTService.class));
                });
                return true;
            });

            findPreference("open_dev_page").setOnPreferenceClickListener(preference -> {
                Intent intent_s = new Intent(this.getContext(), DevActivity.class);
                startActivity(intent_s);
                return true;
            });

        }
    }


}